;(function() {
    function change() {
        var font = document.documentElement.clientWidth / 375 * 50;
        document.documentElement.style.fontSize = font + 'px';
    };
    window.addEventListener('resize', change, false);
    change();
})();
// rem 单位换算 *不可删除
String.prototype.urltoobj = function () {
    var urlobj = {};
    var str = this;
    var start = str.indexOf('?');
    var urlstrdata = str.substring(start+1);
    if(str == urlstrdata){
        return urlstrdata
    }else {
        var urlarrdata = urlstrdata.split('&');
        urlarrdata.map(function (item,idx) {
            var surlobjdata = item.split('=');
            urlobj[surlobjdata[0]] = surlobjdata[1]
        });
        return urlobj
    }
};

var url = window.location.href;
var urlobj = url.urltoobj();



var hasGoods = [];
var needobj = [];

$(function () {
    let world_type = localStorage.getItem('world_type') || 'cn';
    $.ajax({
    	//http://122.115.56.235:8080
        url: '/getGoods',
        type: 'post',
        async: true,
        data: JSON.stringify({world_type: world_type}),
        contentType: 'application/json; charset=UTF-8',
        success: function (msg) {
            var contenthtml = '';
            msg = msg.message;
            needobj = [{'t_name':msg[0].t_name,'g_type': msg[0].g_type,con: []}];
            var navhtml = '';
            msg.map(function (val,idx) {
                if(val.g_type != needobj[needobj.length - 1].g_type){
                    var cot = {'t_name': val.t_name,g_type: val.g_type,con: [{id: val.id,g_name: val.g_name,g_con: val.g_con,g_price:val.g_price,g_num: val.g_num,g_name_en: val.g_name_en,g_has: 0}]};
                    needobj.push(cot)
                }else {
                    needobj[needobj.length - 1].con.push({id: val.id,g_name: val.g_name,g_con: val.g_con,g_price:val.g_price,g_num: val.g_num,g_name_en: val.g_name_en,g_has: 0})
                }
            });

            needobj.map(function (val) {
                if(val.t_name == '热销'){
                    navhtml += '<li class="menu-left-li"><img class="icon" src="img/fire.png">'+ val.t_name +'</li>';
                }else {
                    navhtml += '<li class="menu-left-li">'+ val.t_name +'</li>';
                }
            });
            $('#menuLeft .menu-left-ul').html(navhtml);

            setcont(needobj);
            $('#menuLeft .menu-left-ul li').eq(0).addClass('on');
            $('#menuRight').scroll(function () {
                var t = $(this).scrollTop();
                for (var i in heightArr) {
                    if ((t + $(window).width()/ 375 * 50 *1) >= heightArr[i]){
                        $('#menuLeft .menu-left-li').eq(i).addClass('on').siblings().removeClass('on');
                    }
                }

                $('#menuLeft').stop().animate({
                    scrollTop: $('#menuLeft .menu-left-li').height() * ($('#menuLeft .menu-left-li.on').index()+0.5) - ($('#menuLeft').height()/2)
                }, 200)
                // 左侧按钮位置垂直居中
            });

            $('#menuLeft .menu-left-li').click(function (e) {
                var i = $(this).index();
                var t = $('#menuRight').scrollTop() - $(window).width()/ 375 * 50 * 1 + 1;
                $('#menuLeft .menu-left-li').eq(i).addClass('on').siblings().removeClass('on');
                $('#menuRight').scrollTop( $('#menuRight .content-item').eq(i).offset().top + t);
                // $('#menuLeft .menu-left-li').eq(i).addClass('on').siblings().removeClass('on');
            });
//左边联动
            var heightArr = [];
            for (var i = 0; i < $('#menuRight .content-item').length; i++) {
                heightArr.push($('#menuRight .content-item').eq(i).offset().top);
            }
// 元素相对窗口顶部偏移量存成数组

//右边联动


// model关闭模太框
            $('.model').click(function () {
                claseModel()
            });

// 关闭模太框
            function claseModel(){
                $('.model-all').animate({top: '100%'},200);
                setTimeout(function () {
                    $('.model').css({'display': 'none'});
                },201)
            }
// 关闭或者显示模太框
            $('.total>div:first-child').click(function (event) {
                event.stopPropagation();
                if($('.model').css('display') == 'block'){
                    claseModel();
                }else {
                    $('.model').css({'display': 'block'});
                    $('.model-all').animate({top: '0px'},200);
                }
            });
// 阻止冒泡事件
            $('.model-box').click(function (event) {
                event.stopPropagation();
            });
            $('.sureclearall').click(function () {
                $('.clear-model').css({'display': 'block'});
            });
// 关闭清除模太框
            $('.cancle').click(function () {
                $('.clear-model').css({'display': 'none'})
            });
// 确定清除按钮
            $('.sure-button').click(function () {
                hasGoods = [];
                for(var i = 0; i < needobj.length; i++){
                    for(var j = 0; j < needobj[i].con.length; j++){
                        needobj[i].con[j].g_has = 0;
                    }
                }
                sethasgoods(hasGoods);
                setallprice(hasGoods);
                setcont(needobj);
                $('.clear-model').css({'display': 'none'})
            });

            $('.hasok').click(function () {
                console.log(hasGoods);
                let goodsid = [];
                hasGoods.map(function (val) {
                    goodsid.push({id: val.id,num: val.g_has});
                });
                console.log("23333333",urlobj.t);
                var goods = {'t': 1,'g':goodsid};
                $.ajax({
                    url: '/getPays',
                    method: 'post',
                    async: true,
                    data: JSON.stringify(goods),
                    contentType: 'application/json; charset=UTF-8',
                    success: function (msg) {
                        if(msg.code == 0){
                            window.location.href = 'getPay?c_time=' + msg.c_time + '&o_time=' + msg.o_time;
                        }else {
                            alert('订单生成失败！')
                        }
                    }
                })
                // window.location.href = 'end.html?hasdata=' + urldata.join('/')
            });
        }
    });
    $('.lang-model').click(function () {
        $(this).hide();
    })
});

function openlang() {
    $('.lang-model').show();
}

function setlang(lan) {
    localStorage.setItem('world_type',lan);
    location.reload();
    $('.Language').text(lan);
}

function setcont(data) {
    var conthtml = '';
    data.map(function (val) {

        conthtml += '<div class="content-item"><ul class="shop-list">';
        var listUl = '';
        val.con.map(function (item) {
            var isClass = 'none';
            if(item.g_has != 0){
                isClass = 'inline-block'
            }
            listUl += "<li>" +
                " <img class='shop-list-img' src='img/list1.jpeg'>" +
                " <div class='shop-msg'>" +
                "   <div>" +
                "      <p class='shop-name overhidden'>"+ item.g_name +"</p>" +
                "      <p class='shop-evaluation overhidden'>"+ item.g_con +"</p>" +
                "   </div>" +
                "   <div class='shop-price'><span>&yen; <em>"+ item.g_price +"</em></span>" +
                "      <span class='shop-num'><img src='./img/del.png' style='display: " + isClass + " '  class='del' onclick='delNum(this," + JSON.stringify(item) + ");'><em class='num'>"+ item.g_has +"</em><img src='./img/add.png' onclick='addNum(this,"+JSON.stringify(item)+");'></span>" +
                "   </div>" +
                " </div>" +
                "</li>"
        });
        conthtml += listUl + '</ul></div>';
    });
    $('#menuRight .menu-right-content').html(conthtml);
}





// 添加数量
function addNum(obj,data){
    var num = data.g_has;
    if(num < data.g_num){
        num++;
        if(num > 0){
            for(var i = 0; i < needobj.length; i++){
                for(var j = 0; j < needobj[i].con.length; j++){
                    if(data.id == needobj[i].con[j].id){
                        needobj[i].con[j].g_has = num;
                    }
                }
            }
            $(obj).siblings('.del').css({'display': 'inline-block'});
            $(obj).siblings('.del').addClass('addcla');
        }

        var hasobj = {g_name: data.g_name, g_num: data.g_num, g_has: num, id: data.id,g_price: data.g_price};
        if(hasGoods.length === 0){
            hasGoods.push(hasobj)
        }else {
            var k = null;
            for(var i = 0; i < hasGoods.length; i++){
                if(hasGoods[i].id == data.id){
                    k = i;
                }
            }
            if(k != null){
                hasGoods[k].g_has = num;
            }else {
                hasGoods.push(hasobj)
            }
        }

    }

    sethasgoods(hasGoods);
    setallprice(hasGoods);
    setcont(needobj);

}
// 减少数量
function delNum(obj,data) {
    var num = data.g_has;
    if(num > 0){
        num--;
        var k = 0;
        for(var i = 0; i < hasGoods.length; i++){
            if(hasGoods[i].id == data.id){
                k = i;
            }
        };

        for(var i = 0; i < needobj.length; i++){
            for(var j = 0; j < needobj[i].con.length; j++){
                if(data.id == needobj[i].con[j].id){
                    needobj[i].con[j].g_has = num;
                }
            }
        }

        hasGoods[k].g_has = num;
        if(num <= 0){
            $(obj).css({display: 'none'});
            hasGoods.splice(k,1)
        }
    }
    sethasgoods(hasGoods);
    setallprice(hasGoods);
    setcont(needobj);
}


function sethasgoods(data) {
    var hashtml = '';
    data.map(function (item) {
        hashtml += "<li><span class='overhidden'>"+ item.g_name +"</span><div><span>&yen; <em>"+ Number(item.g_has * item.g_price).toFixed(2) +"</em></span> <span class='shop-num'><img src='./img/del.png' onclick='delNumtwo(this,"+ JSON.stringify(item) +")'  class='del'><em class='num'>" + item.g_has + "</em><img src='./img/add.png' onclick='addNumtwo(this,"+ JSON.stringify(item) +")'></span></div></li>";
    });
    if(data.length == 0){
        hashtml = '<li style="justify-content: center">没有数据</li>'
    }
    $('.model-content ul').html(hashtml)
}

function setallprice(data) {
    var price = 0;
    var goodsnum = 0;
    data.map(function (item) {
        price += item.g_has * item.g_price;
        goodsnum += item.g_has;
    });
    $('#allPrice').html(Number(price).toFixed(2));
    $('.icon-num').html(goodsnum);
}

function addNumtwo(obj,data) {
    var hasnum = data.g_has + 1;
    if(hasnum <= data.g_num){
        for(var i = 0; i < needobj.length; i++){
            for(var j = 0; j < needobj[i].con.length; j++){
                if(data.id == needobj[i].con[j].id){
                    if(hasnum <= 0){
                        needobj[i].con[j].g_has = hasnum;
                    }else {
                        needobj[i].con[j].g_has = hasnum;
                    }
                }
            }
        }

        hasGoods.map(function (item,idx) {
            if(item.id == data.id){
                hasGoods[idx].g_has = hasnum;
            }
        });

    }
    setcont(needobj);
    sethasgoods(hasGoods);
    setallprice(hasGoods)
}

function delNumtwo(obj,data) {
    var hasnum = data.g_has - 1;
    if(hasnum >= 0){
        for(var i = 0; i < needobj.length; i++){
            for(var j = 0; j < needobj[i].con.length; j++){
                if(data.id == needobj[i].con[j].id){
                    if(hasnum <= 0){
                        needobj[i].con[j].g_has = hasnum;
                    }else {
                        needobj[i].con[j].g_has = hasnum;
                    }
                }
            }
        }

        hasGoods.map(function (item,idx) {
            if(item.id == data.id){
                if(hasnum == 0){
                    hasGoods.splice(idx,1);
                }else {
                    hasGoods[idx].g_has = hasnum;
                }

            }
        });
        setcont(needobj);
        sethasgoods(hasGoods);
        setallprice(hasGoods)
    }

}

